package com.example.blue;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class MagResActivity extends AppCompatActivity {
    private static final String TAG = "MagResActivity";

    private String deviceId;
    private String magResult;
    private TextView tvx1;
    private TextView tvy1;
    private TextView tvz1;
    private TextView tvxs1;
    private TextView tvys1;
    private TextView tvzs1;

    private TextView tvx2;
    private TextView tvy2;
    private TextView tvz2;
    private TextView tvxs2;
    private TextView tvys2;
    private TextView tvzs2;

    private TextView tvx3;
    private TextView tvy3;
    private TextView tvz3;
    private TextView tvxs3;
    private TextView tvys3;
    private TextView tvzs3;

    private TextView tvx4;
    private TextView tvy4;
    private TextView tvz4;
    private TextView tvxs4;
    private TextView tvys4;
    private TextView tvzs4;

    private TextView tvx5;
    private TextView tvy5;
    private TextView tvz5;
    private TextView tvxs5;
    private TextView tvys5;
    private TextView tvzs5;

    private TextView tvx6;
    private TextView tvy6;
    private TextView tvz6;
    private TextView tvxs6;
    private TextView tvys6;
    private TextView tvzs6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CrashHandler.getInstance().init(this);
        setContentView(R.layout.activity_mag_res);
        // 开启返回箭头
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(null);
        getSupportActionBar().setTitle("磁力计校准结果");

        Intent intent = getIntent();
        deviceId = intent.getStringExtra("deviceId");
        magResult = intent.getStringExtra("magResult");
        Log.i(TAG,magResult);
        if (magResult == null || magResult.isEmpty()) {
            Toast.makeText(MagResActivity.this,"校准结果未拿到",Toast.LENGTH_SHORT).show();
        }
        String[] magres = magResult.split("\n");
        System.out.println("magcor"+magres[0]+" "+magres[1]+" "+magres[2]);
        tvx1=findViewById(R.id.sensor1xa);
        tvy1=findViewById(R.id.sensor1ya);
        tvz1=findViewById(R.id.sensor1za);
        tvxs1=findViewById(R.id.sensor1xj);
        tvys1=findViewById(R.id.sensor1yj);
        tvzs1=findViewById(R.id.sensor1zj);

        tvx2=findViewById(R.id.sensor2xa);
        tvy2=findViewById(R.id.sensor2ya);
        tvz2=findViewById(R.id.sensor2za);
        tvxs2=findViewById(R.id.sensor2xj);
        tvys2=findViewById(R.id.sensor2yj);
        tvzs2=findViewById(R.id.sensor2zj);

        tvx3=findViewById(R.id.sensor3xa);
        tvy3=findViewById(R.id.sensor3ya);
        tvz3=findViewById(R.id.sensor3za);
        tvxs3=findViewById(R.id.sensor3xj);
        tvys3=findViewById(R.id.sensor3yj);
        tvzs3=findViewById(R.id.sensor3zj);

        tvx4=findViewById(R.id.sensor4xa);
        tvy4=findViewById(R.id.sensor4ya);
        tvz4=findViewById(R.id.sensor4za);
        tvxs4=findViewById(R.id.sensor4xj);
        tvys4=findViewById(R.id.sensor4yj);
        tvzs4=findViewById(R.id.sensor4zj);

        tvx5=findViewById(R.id.sensor5xa);
        tvy5=findViewById(R.id.sensor5ya);
        tvz5=findViewById(R.id.sensor5za);
        tvxs5=findViewById(R.id.sensor5xj);
        tvys5=findViewById(R.id.sensor5yj);
        tvzs5=findViewById(R.id.sensor5zj);

        tvx6=findViewById(R.id.sensor6xa);
        tvy6=findViewById(R.id.sensor6ya);
        tvz6=findViewById(R.id.sensor6za);
        tvxs6=findViewById(R.id.sensor6xj);
        tvys6=findViewById(R.id.sensor6yj);
        tvzs6=findViewById(R.id.sensor6zj);


        tvx1.setText("X轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[0])));
        tvy1.setText("Y轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[1])));
        tvz1.setText("Z轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[2])));
        tvxs1.setText("比例因子xS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[3])));
        tvys1.setText("比例因子yS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[4])));
        tvzs1.setText(" 比例因子zS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[5])));

        tvx2.setText("X轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[6])));
        tvy2.setText("Y轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[7])));
        tvz2.setText("Z轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[8])));
        tvxs2.setText("比例因子xS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[9])));
        tvys2.setText("比例因子yS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[10])));
        tvzs2.setText(" 比例因子zS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[11])));

        tvx3.setText("X轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[12])));
        tvy3.setText("Y轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[13])));
        tvz3.setText("Z轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[14])));
        tvxs3.setText("比例因子xS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[15])));
        tvys3.setText("比例因子yS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[16])));
        tvzs3.setText(" 比例因子zS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[17])));

        tvx4.setText("X轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[18])));
        tvy4.setText("Y轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[19])));
        tvz4.setText("Z轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[20])));
        tvxs4.setText("比例因子xS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[21])));
        tvys4.setText("比例因子yS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[22])));
        tvzs4.setText(" 比例因子zS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[23])));

        tvx5.setText("X轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[24])));
        tvy5.setText("Y轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[25])));
        tvz5.setText("Z轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[26])));
        tvxs5.setText("比例因子xS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[27])));
        tvys5.setText("比例因子yS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[28])));
        tvzs5.setText(" 比例因子zS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[29])));

        tvx6.setText("X轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[30])));
        tvy6.setText("Y轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[31])));
        tvz6.setText("Z轴偏差："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[32])));
        tvxs6.setText("比例因子xS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[33])));
        tvys6.setText("比例因子yS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[34])));
        tvzs6.setText(" 比例因子zS："+String.format(Locale.US, "%.2e",  Double.parseDouble(magres[35])));
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish(); // 结束当前Activity，返回上一个
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}